<?php

namespace app\health\validate;

use app\health\model\HealthUserAddress;

class OrderValidate extends BaseValidate
{
    protected $rule = [
        'order_id'      => 'require',
        'confirm_type'  => 'require|in:1,2',
        'cart_id'       => 'requireIf:confirm_type,1',
        'number'        => 'requireWith:goods_attr_id',
        'goods_attr_id' => 'requireIf:confirm_type,2',
        'address_id'    => 'require|addressExist'
    ];

    protected $message = [
        'confirm_type.require'    => '请选择结算模式',
        'confirm_type.in'         => '结算模式不正确',
        'cart_id.requireIf'       => '请选择结算的购物车商品',
        'goods_attr_id.requireIf' => '请选择商品规格',
        'number.requireWith'      => '请输入购物买数量',
        'number.min'              => '最少购买数量为1',
        'cart_id.array'           => '非法参数',
        'cart_id.min'             => '最少选择一个商品',
        'address_id.require'      => '请选择收货地址',
        'order_id.require'        => '请选择订单id'
    ];

    // 场景验证
    protected $scene = [
        // 确认订单信息
        'confirm_info'    => ['confirm_type', 'cart_id', 'number', 'goods_attr_id'],
        // 创建订单
        'create_order'    => ['confirm_type', 'cart_id', 'number', 'goods_attr_id', 'address_id'],
        //取消订单
        'cancel'          => ['order_id'],
        //确认收货
        'confirm_receipt' => ['order_id'],
        'detail'          => ['order_id'],
        //支付订单
        'pay'             => ['order_id']
    ];

    public function addressExist($value, $rule, $data)
    {
        $address = HealthUserAddress::where('id', $value)->where('user_id', $data['user_id'])->find();
        if (!$address) {
            return '地址不存在,请重新选择';
        }
        return true;
    }
}
